package com.ming.myqqsliadingmenu;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.nineoldandroids.view.ViewHelper;


public class MyQQSlidingMenu extends HorizontalScrollView {
    //屏幕的宽
    private int mScreenWidth;
    //包裹左侧菜单和内容的视图
    private LinearLayout mWapper;
    //左侧菜单视图
    private LinearLayout leftMenu;
    //内容区域视图
    private LinearLayout content;
    //菜单视图完全显示时(单位为dip)
    private int mMenuRightPadding = 100;
    //是否首次绘制视图，只允许绘制一次视图
    private boolean once = true;
    //左侧菜单视图宽度
    private int mMenuWidth;
    //左侧菜单栏是否显示
    private boolean isOpen = false;
    private String TAG = "MyQQSlidingMenu";

    public MyQQSlidingMenu(Context context, AttributeSet attrs) {
        super(context, attrs);

        measureWidth(context);
    }

    public MyQQSlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        measureWidth(context);
    }

    /**
     * 测量屏幕的宽度
     * @param context
     */
    private void measureWidth(Context context){
        //获取到屏幕的宽
        WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;
    }

    /**
     * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (once) {
            mWapper = (LinearLayout) getChildAt(0);
            leftMenu = (LinearLayout) mWapper.getChildAt(0);
            content = (LinearLayout) mWapper.getChildAt(1);
            //降dip值转换成px
            mMenuRightPadding = dip2px(getContext(),mMenuRightPadding);
            //设置左侧菜单的宽度，保留一定宽度显示一部分内容视图
            mMenuWidth = leftMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
            //设置内容视图宽度
            content.getLayoutParams().width = mScreenWidth;
            //设置成false，让其不再进行测量绘制
            once = false;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        if (changed) {
            this.scrollTo(mMenuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

            case MotionEvent.ACTION_UP:
                //隐藏的宽度
                int scrollX = getScrollX();
                //隐藏的宽度如果小于屏幕宽度的一半则弹出左侧菜单
                if(scrollX <= (mMenuWidth/2)){
                    this.smoothScrollTo(0,0);
                    isOpen = true;
                }else{
                    this.smoothScrollTo(mMenuWidth,0);
                    isOpen = false;
                }
                return true;

            default:
                break;

        }
        return super.onTouchEvent(ev);

    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        //动画的梯度值，从1~0；
        float scale = l*1.0f/mMenuWidth;
        //实现了移动内容时左侧菜单仿佛是从后面出来的效果
        ViewHelper.setTranslationX(leftMenu,mMenuWidth*scale*0.6f);
        /**
         * 1：内容区域1.0~0.7 缩放的效果 scale : 1.0~0.0
         * 3：菜单的显示时有缩放以及透明度变化 缩放：0.7 ~1.0  透明度 0.6 ~ 1.0
         */
        //左侧菜单缩放0.7~1.0
        float leftMenuScale = 1.0f-0.3f*scale;
        //左侧菜单透明0.6 ~ 1.0
        float leftMenuAlpha = 1.0f-0.5f*scale;
        //内容缩放1.0~0.7
        float contentScale = 0.7f+0.3f*scale;

        //给内容区域设置缩放的中心点在左边的中间点
        ViewHelper.setPivotX(content,0);
        ViewHelper.setPivotY(content,content.getHeight()/2);
        //缩放内容
        ViewHelper.setScaleX(content,contentScale);
        ViewHelper.setScaleY(content, contentScale);

        //左侧菜单缩放
        ViewHelper.setScaleX(leftMenu,leftMenuScale);
        ViewHelper.setScaleY(leftMenu,leftMenuScale);

        //左侧菜单透明度
        ViewHelper.setAlpha(leftMenu,leftMenuAlpha);
    }

    /**
     * 开关左侧菜单
     */
    public void toggleMenu(){
        if(isOpen){
            //菜单是打开的，则关闭掉
            this.smoothScrollTo(mMenuWidth,0);
            isOpen = false;
        }else{
            //菜单是关闭的，则打开
            this.smoothScrollTo(0,0);
            isOpen = true;
        }
    }

}
